package com.sy.servlet;

import com.sy.entity.User;
import com.sy.service.CollectService;
import com.sy.service.impl.CollectServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

import static com.sy.constant.BusinessConstant.SESSION_USER;

@WebServlet("/collect/*")
public class CollectServlet extends AbstractBaseServlet {
    private CollectService collectService = new CollectServiceImpl();

    @Override
    protected Object doService(String action, HttpServletRequest req, HttpServletResponse res) throws Exception {
        //获取指定用户的收藏车辆ID
        if ("getByUserId".equals(action)) {
            User user = getSessionAttribute(req, SESSION_USER);
            Integer userId = null;
            if (Objects.nonNull(user)) {
                userId = user.getId();
            }
            return collectService.getCollectCarIdsByUserId(userId);
        }
        //收藏或取消收藏
        if ("addOrCancel".equals(action)) {
            User user = getSessionAttribute(req, SESSION_USER);
            Integer userId = null;
            if (Objects.nonNull(user)) {
                userId = user.getId();
            }
            return collectService.addOrCancelCollect(userId, paramToInt(req, "carId"));
        }
        return null;
    }
}
